package org.mq.mymq.broker.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @ClassName FileContentReaderUtils
 * @Description 简化版本的文件读取工具
 * @Author jiarong_ye
 * @Date 2025/6/15 17:36
 * @Version 1.0
 */
public class FileContentUtil {
    public static String readFromFile(String path) {
        try (BufferedReader in = new BufferedReader(new FileReader(path))) {
            StringBuffer strb = new StringBuffer();
            while (in.ready()) {
                strb.append(in.readLine());
            }
            return strb.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件覆盖写入
     *
     * @param path 文件路径
     * @param content 写入内容
     */
    public static void overWriteToFile(String path, String content) {
        try(FileWriter fw = new FileWriter(path);) {
            fw.write(content);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

//    public static void main(String[] args) {
//        String content = FileContentReaderUtil.readFromFile("F:\\maven-proj\\mq\\broker\\config\\mymq-topic.json");
//        List<MyMqTopicModel> myMqTopicModels = JSON.parseArray(content, MyMqTopicModel.class);
//        System.out.println(myMqTopicModels);
//    }
}
